﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using nwrole.lib;

namespace nwrole.service
{
    internal static class Configuration
    {
        public static List<Plugin> Plugins = new List<Plugin>();
        public static List<Solution> Solutions = new List<Solution>();
        public static List<Solution.Service> Services = new List<Solution.Service>();
        public static List<Solution.Instance> Instances = new List<Solution.Instance>();

        #region Loading and saving
        public static void Load ()
        {
            if (!File.Exists(ConfigurationFilePath))
                return;

            PropertyList plist = new PropertyList(ConfigurationFilePath);

            // Plugins
            foreach (object dict in plist.ArrayForKey("plugins"))
                Plugins.Add(StorageUtil.PluginFromDictionary((IDictionary)dict));

            // Solutions
            foreach (object dict in plist.ArrayForKey("solutions"))
                Solutions.Add(StorageUtil.SolutionFromDictionary((IDictionary)dict));
        }

        public static void Save ()
        {
            try
            {
                Directory.CreateDirectory(ConfigurationDirectoryPath);
                PropertyList plist = new PropertyList();

                // Plugins
                plist["plugins"] = new List<object>();
                foreach (Plugin plugin in Plugins)
                    plist.ArrayForKey("plugins").Add(StorageUtil.PluginToDictionary(plugin));

                // Solutions
                plist["solutions"] = new List<object>();
                foreach (Solution solution in Solutions)
                    plist.ArrayForKey("solutions").Add(StorageUtil.SolutionToDictionary(solution));

                plist.Save(ConfigurationFilePath);
            }
            catch (Exception exception)
            {
                EventLogger.LogException("Configuration.Save", exception);
            }
        }
        #endregion

        #region Paths
        public static string RootDirectoryPath
        {
            get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), SupervisorService.ApplicationDirectory); }
        }

        public static string ConfigurationDirectoryPath
        {
            get { return Path.Combine(RootDirectoryPath, "configuration"); }
        }

        public static string ConfigurationFilePath
        {
            get { return Path.Combine(ConfigurationDirectoryPath, "configuration.plist"); }
        }

        public static string PluginsDirectory
        {
            get { return Path.Combine(ConfigurationDirectoryPath, "plugins"); }
        }

        public static string SolutionsDirectory
        {
            get { return Path.Combine(ConfigurationDirectoryPath, "solutions"); }
        }
        #endregion

        #region Plugins
        public static Plugin PluginWithId (Guid id)
        {
            return (from plugin in Plugins where plugin.Id == id select plugin).FirstOrDefault();
        }
        #endregion

        #region Solutions
        public static Solution SolutionWithId (Guid id)
        {
            return (from solution in Solutions where solution.Id == id select solution).FirstOrDefault();
        }
        #endregion

        #region Services
        public static Solution.Service ServiceWithId(Guid serviceId)
        {
            return (from service in Services where service.Id == serviceId select service).FirstOrDefault();
        }

        public static List<Solution.Service> ServicesInSolution (Guid solutionId)
        {
            return (from service in Services where service.SolutionId == solutionId select service).ToList();
        }
        #endregion

        #region Instances
        public static Solution.Instance InstanceWithId(Guid id)
        {
            return (from instance in Instances where instance.Id == id select instance).FirstOrDefault();
        }

        public static List<Solution.Instance> InstancesInPlugin(Guid pluginId)
        {
            return (from instance in Instances where instance.PluginId == pluginId select instance).ToList();
        }

        public static List<Solution.Instance> InstancesInSolution(Guid solutionId)
        {
            return (from instance in Instances where instance.SolutionId == solutionId select instance).ToList();
        }

        public static List<Solution.Instance> InstancesInService (Guid serviceId)
        {
            return (from instance in Instances where instance.ServiceId == serviceId select instance).ToList();
        }
        #endregion
    }
}
